/**
 * Copyright (c) 2016,http://www.365wuliu.com/  All Rights Reserved.
 */
package com.cargo.base.file;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.cargo.base.constants.CommonConsts;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/** 
 * AliyunFileProcessor:基于OSS存储的阿里云文件处理类
 * @Project Name:edi 
 * @File Name:AliyunFileProcessor.java 
 * @Package Name:com.cargo.base.file
 * @Creator:wizard
 * @Date:2016年10月18日下午1:58:44 
 */
public class AliyunFileProcessor {
	//OSS服务的endPoint
	private static final String END_POINT = "http://oss-cn-beijing.aliyuncs.com";
	//访问OSS的Access Key ID
	private static final String ACCESS_KEY_ID = "LTAIBB38o2EexwYm";
	//访问OSS的Secret Access Key
	private static final String SECRET_ACCESS_KEY = "HhMJS0Sh6jODG6DosNseBKtvE7peCQ";
	//bucketName
	private static final String BUCKET_NAME = "wizardfirstframe"; // TODO
	
	/**
	 * 
	 * genFileFullPath:生成阿里云文件的全路径
	 * @param fileName
	 * @param args
	 * @return 
	 * @return :String 
	 * @Creator:wizard
	 * @Date:2016年10月18日 下午5:19:39
	 */
	public static String genFileFullPath(String fileName, String...args) {
		//取得上传阿里云文件的根目录
		String path = CommonConsts.EMPTY_STRING;
		//遍历上传文件路径
		for(String arg : args) {
			path += arg + CommonConsts.SLASH;
		}
		//拼接文件名
		path += fileName;
		
		return path;
	}
	
	/**
	 * 
	 * uploadFile:上传文件到阿里云服务器(上传文件流)
	 * @param fileName 带路径的文件名
	 * @param file 
	 * @return :void 
	 * @Creator:wizard
	 * @Date:2016年10月19日 下午5:05:41
	 */
	public static void uploadFile(String fileName, MultipartFile file) {
		OSSClient client = new OSSClient(END_POINT, ACCESS_KEY_ID, SECRET_ACCESS_KEY);
		try {
			client.putObject(BUCKET_NAME, fileName, file.getInputStream());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(client != null) {
				client.shutdown();
			}
		}
	}
	
	/**
	 * 
	 * uploadFile:上传文件到阿里云服务器(上传文件流)
	 * @param fileName 带路径的文件名
	 * @param file 
	 * @return :void 
	 * @Creator:wizard
	 * @Date:2016年10月19日 下午5:05:41
	 */
	public static String uploadFiles(String fileName, MultipartFile file) {
		String fileUrl = "";
		OSSClient client = new OSSClient(END_POINT, ACCESS_KEY_ID, SECRET_ACCESS_KEY);
		try {
			client.putObject(BUCKET_NAME, fileName, file.getInputStream());
			fileUrl = BUCKET_NAME + "." + END_POINT.split("//")[1] + "/" + fileName;
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if(client != null) {
					client.shutdown();
				}
			}
		return fileUrl;
	}
	
	/**
	 * 
	 * uploadFile:上传文件到阿里云服务器(文件输入流)
	 * @param fileName
	 * @param inputStream 
	 * @return :void 
	 * @Creator:wizard
	 * @Date:2016年11月3日 下午4:22:20
	 */
	public static void uploadFile(String fileName, InputStream inputStream) {
		OSSClient client = new OSSClient(END_POINT, ACCESS_KEY_ID, SECRET_ACCESS_KEY);
		
		client.putObject(BUCKET_NAME, fileName, inputStream);
		client.shutdown();
	}
	
	/**
	 * 
	 * uploadFile:上传文件到阿里云服务器(文件)
	 * @param fileName
	 * @param file 
	 * @return :void 
	 * @Creator:wizard
	 * @Date:2016年11月3日 下午3:27:25
	 */
	public static void uploadFile(String fileName, File file) {
		OSSClient client = new OSSClient(END_POINT, ACCESS_KEY_ID, SECRET_ACCESS_KEY);
	try {
			client.putObject(BUCKET_NAME, fileName, file);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(client != null) {
				client.shutdown();
			}
		}
	}
	
	/**
	 * 
	 * downloadFile:阿里云服务器文件下载
	 * @param aliFileName
	 * @param response
	 * @throws Exception
	 * @return :void 
	 * @Creator:wizard
	 * @Date:2016年10月19日 下午5:11:54
	 */
	public static void downloadFile(String aliFileName, HttpServletResponse response) throws Exception {
		OSSClient client = new OSSClient(END_POINT, ACCESS_KEY_ID, SECRET_ACCESS_KEY);
		InputStream is = null;
		OutputStream os = null;
		try {
			//通过OSS从阿里云服务器取得文件对象
			OSSObject ossObj = client.getObject(BUCKET_NAME, aliFileName);
			//读取文件内容
			is = ossObj.getObjectContent();
			os = response.getOutputStream();
			
			byte[] b = new byte[2048];
			int length;
			while ((length = is.read(b)) > 0) {
				os.write(b, 0, length);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(os != null) {
				os.close();
			}
			if(is != null) {
				is.close();
			}
			client.shutdown();
		}
	}
}
